package cn.lutu.adapter.controller;

import cn.lutu.dao.mapper.ArticleMapper;
import cn.lutu.domain.service.ArticleService;
import cn.lutu.protocol.dto.ArticleCollectDTO;
import cn.lutu.protocol.dto.ArticlePostDTO;
import cn.lutu.protocol.dto.ArticleUpDTO;
import cn.lutu.protocol.vo.*;
import cn.lutu.security.jwtFilter.CurrentPrincipal;
import cn.lutu.util.pojo.vo.PageData;
import cn.lutu.util.response.ResultVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Slf4j
@Api(tags = "文章模块")
@RestController
@RequestMapping("/v1/contents/")
public class ArticleController {
    @Autowired
    ArticleMapper mapper;

    @Autowired
    ArticleService service;

    @ApiOperation("1.发表文章")
    @ApiOperationSupport(order = 1)
    @PostMapping("post")
    public ResultVO post(ArticlePostDTO articlePostDTO,
                         @AuthenticationPrincipal @ApiIgnore CurrentPrincipal currentPrincipal) throws Exception {
        log.debug("处理发表文章业务,参数:{}",articlePostDTO);
        service.add_new(articlePostDTO,currentPrincipal.getId());
        return ResultVO.ok();
    }

    @ApiOperation("2.获取全部文章列表")
    @ApiOperationSupport(order = 2)
    @GetMapping("list")
    public ResultVO selectAll(Integer page,String queryType){
        log.debug("开始处理全部文章列表业务,页码:{}",page,queryType);
        if (page == null) {
            page = 1;
        }
        Integer pageNum = page > 0 ? page : 1;
        PageData<ArticleListVO> pageData ;
        if ("all".equals(queryType)) {
            pageData = service.selectAll(1, Integer.MAX_VALUE);
        } else {
            pageData = service.selectAll(pageNum);
        }
        log.info("查询结束，查询结果为{}",pageData);
        return ResultVO.ok(pageData);
    }

    @ApiOperation("3.获取热门文章列表")
    @ApiOperationSupport(order = 3)
    @GetMapping("hotArt")
    public ResultVO selectHotArt(){
        log.debug("开始处理热门文章列表业务");
        List<ArticleListVO> list = service.selectHotArt();
        return ResultVO.ok(list);
    }

    @ApiOperation("4.获取最近文章列表")
    @ApiOperationSupport(order = 4)
    @GetMapping("recentList")
    public ResultVO selectRecent(){
        log.debug("开始处理最近文章列表业务");
        List<ArticleListVO> list = service.selectRecent();
        return ResultVO.ok(list);
    }

    @ApiOperation("5.获取热搜标题")
    @ApiOperationSupport(order = 5)
    @GetMapping("hot")
    public ResultVO hot(){
        log.debug("开始处理标题列表业务");
        List<HotListVO> list = service.selectHot();
        return ResultVO.ok(list);
    }

    @ApiOperation("6.获取详情信息")
    @ApiOperationSupport(order = 6)
    @GetMapping("/detail")
    public ResultVO articleDetail( Long id,Long userId){
        log.debug("开始处理文章详情业务,参数:{}",id,userId);
        ArticleDetailVO articleDetailVO = service.selectArticleDetail(id,userId);
        return ResultVO.ok(articleDetailVO);
    }

    @ApiOperation("7.收藏文章相关")
    @ApiOperationSupport(order = 7)
    @PostMapping("collect")
    public ResultVO collect(ArticleCollectDTO articleCollectDTO){
        log.debug("开始处理收藏文章相关业务,参数:{}",articleCollectDTO);
        service.collect(articleCollectDTO);
        return ResultVO.ok();
    }


    @ApiOperation("8.点赞请求")
    @ApiOperationSupport(order = 8)
    @PostMapping("up")
    public ResultVO up(ArticleUpDTO articleUpDTO){
        log.debug("正在处理获取点赞数业务,参数:{}",articleUpDTO);
        service.up(articleUpDTO);
        return ResultVO.ok();
    }


    @ApiOperation("9.删除文章")
    @ApiOperationSupport(order = 9)
    @PostMapping("{id}/delete")
    public ResultVO deleteById(@PathVariable Long id){
        log.debug("正在处理删除文章业务,参数:{}",id);
        service.deleteById(id);
        return ResultVO.ok();
    }

    @ApiOperation("10.获取作者其他文章")
    @ApiOperationSupport(order = 10)
    @GetMapping("others")
    public ResultVO others(Integer page, String queryType, Long userId){
        log.debug("开始处理获取作者其他文章业务,页码：{},UserId:{}", page,userId);
        if (page == null) {
            page = 1;
        }
        Integer pageNum = page > 0 ? page : 1;
        PageData<ArticleOthersListVO> pageData ;
        if ("all".equals(queryType)) {
            pageData = service.selectByUserId(1, Integer.MAX_VALUE,userId);
        } else {
            pageData = service.selectByUserId(pageNum,userId);
        }
        log.info("查询结束，查询结果为{}",pageData);
        return ResultVO.ok(pageData);
    }

    @ApiOperation("11.获取个人点赞的文章列表")
    @ApiOperationSupport(order = 11)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "queryType", value = "查询类型，当需要查询全部数据时，此参数值应该是all")
    })
    @GetMapping("myArticleUp")
    public ResultVO myArticleUp(Integer page, String queryType, Long userId){
        log.debug("开始处理【查询个人点赞列表】请求，页码：{}", page);
        if (page == null) {
            page = 1;
        }
        Integer pageNum = page > 0 ? page : 1;
        PageData<ArticleOthersListVO> pageData ;
        if ("all".equals(queryType)) {
            pageData = service.selectArticleUp(1, Integer.MAX_VALUE,userId);
        } else {
            pageData = service.selectArticleUp(pageNum,userId);
        }
        log.info("查询结束，查询结果为{}",pageData);
        return ResultVO.ok(pageData);
    }

    @ApiOperation("12.获取个人收藏的文章列表")
    @ApiOperationSupport(order = 12)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", dataType = "int"),
            @ApiImplicitParam(name = "queryType", value = "查询类型，当需要查询全部数据时，此参数值应该是all")
    })
    @GetMapping("myArticleCollect")
    public ResultVO myArticleCollect(Integer page, String queryType, Long userId){
        log.debug("获取个人收藏的文章列表");
        log.debug("开始处理【查询个人收藏列表】请求，页码：{}", page);
        if (page == null) {
            page = 1;
        }
        Integer pageNum = page > 0 ? page : 1;
        PageData<ArticleOthersListVO> pageData ;
        if ("all".equals(queryType)) {
            pageData = service.selectArticleCollect(1, Integer.MAX_VALUE,userId);
        } else {
            pageData = service.selectArticleCollect(pageNum,userId);
        }
        log.info("查询结束，查询结果为{}",pageData);
        return ResultVO.ok(pageData);
    }

    @ApiOperation("13.搜索游记")
    @ApiOperationSupport(order = 13)
    @GetMapping("{textSearch}/search")
    public ResultVO search(@PathVariable String textSearch){
        log.debug("正在处理搜索游记业务!,参数:{}",textSearch);
        List<ArticleSearchStandardVO> list = service.selectSearch(textSearch);
        return ResultVO.ok(list);
    }


    @ApiOperation("14.获取待审核文章列表")
    @ApiOperationSupport(order = 14)
    @GetMapping("unCheckList")
    public ResultVO selectUnCheck(Integer page,String queryType){
        log.debug("开始处理获取待审核文章列表业务,页码:{}",page);
        if (page == null) {
            page = 1;
        }
        Integer pageNum = page > 0 ? page : 1;
        PageData<ArticleListVO> pageData ;
        if ("all".equals(queryType)) {
            pageData = service.selectUnCheck(1, Integer.MAX_VALUE);
        } else {
            pageData = service.selectUnCheck(pageNum);
        }
        log.info("查询结束，查询结果为{}",pageData);
        return ResultVO.ok(pageData);
    }

    @ApiOperation("15.审核文章业务")
    @ApiOperationSupport(order = 15)
    @PostMapping("/checkSuccess")
    public ResultVO updateCheck(Long articleId,String checkRemarks){
        log.debug("开始处理审核文章的功能,参数id为{},remarks为{}",articleId,checkRemarks);
        service.updateCheck(articleId,checkRemarks);
        return ResultVO.ok();
    }


    @ApiOperation("14.获取未审核通过文章列表")
    @ApiOperationSupport(order = 14)
    @GetMapping("rejectCheckList")
    public ResultVO rejectCheckList(){
        List<ArticleListVO> list = service.rejectCheckList();
        log.debug("查询结果为{}",list);
        return ResultVO.ok(list);
    }

    @ApiOperation("15.获取个人审核不通过文章列表")
    @ApiOperationSupport(order = 15)
    @GetMapping("rejectCheckListById")
    public ResultVO rejectCheckListById(Long userId){
        log.debug("获取个人审核不通过文章列表,参数:{}",userId);
        List<ArticleListVO> list = service.rejectCheckListById(userId);
        return ResultVO.ok(list);
    }

}
